package com.ruoyi.web.controller.api.city;


import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.city.*;
import com.ruoyi.system.domain.city.vo.TradeOptionVo;
import com.ruoyi.system.domain.city.vo.TradeSecondOptionVo;
import com.ruoyi.system.service.city.*;
import com.ruoyi.web.core.config.SearchRedisHelper;
import com.ruoyi.web.core.utils.UserRecentSearch;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.*;

/**
 * 首页，类别，合作，项目,资讯 Controller
 *
 * @author cjq
 * @date 2023-07-25
 */
@RestController
@RequestMapping("/api/city/home")
public class CityHomeController extends BaseController {


    @Autowired
    private ICityTagService cityTagService;

    @Autowired
    private ICityModuleService cityModuleService;

    @Autowired
    private ICityCarouselService cityCarouselService;

    @Autowired
    private ICityProjectService cityProjectService;

    @Autowired
    private ICityTradeService cityTradeService;

    @Autowired
    private ICityTradeSecondService cityTradeSecondService;

    @Autowired
    private ICityApplyService cityApplyService;

    @Autowired
    private ICityNoticeService cityNoticeService;

    @Autowired
    private SearchRedisHelper searchRedisHelper;

    @Autowired
    private ICityUserService cityUserService;

    @Autowired
    private ICityInfoService cityInfoService;


    /**
     * 查询资讯列表
     */
    @GetMapping("/infoList")
    public TableDataInfo list(CityInfo cityInfo)
    {
        startPage();
        List<CityInfo> list = cityInfoService.selectCityInfoList(cityInfo);
        return getDataTable(list);
    }


    /**
     * 获取模块详细信息
     */
    @GetMapping(value = "/module/{moduleId}")
    public AjaxResult getModule(@PathVariable("moduleId") Long moduleId)
    {
        return success(cityModuleService.selectCityModuleByModuleId(moduleId));
    }

    /**
     * 获取滚动通知详细信息
     */
    @GetMapping(value = "/notice/{noticeId}")
    public AjaxResult getNotice(@PathVariable("noticeId") Long noticeId)
    {
        return success(cityNoticeService.selectCityNoticeByNoticeId(noticeId));
    }

    /**
     * 获取轮播详细信息
     */
    @GetMapping(value = "/carousel/{carouselId}")
    public AjaxResult getCarousel(@PathVariable("carouselId") Long carouselId)
    {
        return success(cityCarouselService.selectCityCarouselByCarouselId(carouselId));
    }

    /**
     * 获取项目详细信息
     */
    @GetMapping(value = "/project/{projectId}")
    public AjaxResult getInfo(@PathVariable("projectId") Long projectId)
    {
        return success(cityProjectService.selectCityProjectByProjectId(projectId));
    }


    /**
     * 查询滚动通知列表
     */

    @GetMapping("/noticeList")
    public List<CityNotice> noticeList(CityNotice cityNotice)
    {

        List<CityNotice> list = cityNoticeService.selectCityNoticeList(cityNotice);
        return list;
    }

    /**
     * 查询轮播列表
     */
    @GetMapping("/carouselList")
    public List<CityCarousel> carouselList(CityCarousel cityCarousel) {

        List<CityCarousel> list = cityCarouselService.selectCityCarouselList(cityCarousel);
        return list;
    }


    /**
     * 查询模块集合
     */
    @GetMapping("/moduleList")
    public List<CityModule> cityModuleList(CityModule cityModule) {

        List<CityModule> list = cityModuleService.selectCityModuleList(cityModule);
        if ( list.size()<40){
            return  list;
        }else {
            return list.subList(0,40);
        }
    }

    /**
     * 查询标签集合
     */
    @GetMapping("/tagList")
    public List<CityTag> tagList(CityTag cityTag) {
        List<CityTag> list = cityTagService.selectCityTagList(cityTag);
        return list;
    }

    /**
     * 查询项目分页列表
     */
    @GetMapping("/projectPage")
    public Map<String,Object> projectPage(UserRecentSearch userRecentSearch)
    {
        CityProject cityProject=new CityProject();
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("user_id", userRecentSearch.getUserId());
        CityUser cityUserDB = cityUserService.getOne(queryWrapper);
        if (cityUserDB!=null){
            userRecentSearch.setUserId(cityUserDB.getUserId());
            userRecentSearch.setUnionId(cityUserDB.getUserId());
        }
        cityProject.setProjectName(userRecentSearch.getSearchInfo());
        cityProject.setProjectTag(userRecentSearch.getSearchInfo());
        cityProject.setProjectIndustry(userRecentSearch.getSearchInfo());
        cityProject.setProjectMode(userRecentSearch.getSearchInfo());
        searchRedisHelper.addRedisRecentSearch(userRecentSearch);
        startPage();
        List<CityProject> list = cityProjectService.selectCityProjectList(cityProject);
        searchRedisHelper.addRedisHotSearch(list);
        Map<String,Object> map=new HashMap<>();
        map.put("list",getDataTable(list));
        map.put("num", Math.ceil(getDataTable(list).getTotal()/10)+1);
        return map;
    }

    /**
     * 热搜列表
     * @return
     */
    @GetMapping("/listHotSearch")
    public Set<CityProject> listHotSearch() {
        Set<CityProject> cityProjectSet=searchRedisHelper.listHotSearch();
        return cityProjectSet;
    }

    /**
     * 查询项目集合
     */
    @GetMapping("/projectList")
    public  List<CityProject> projectList(CityProject cityProject) {

        List<CityProject> list = cityProjectService.selectCityProjectList(cityProject);
        if ( list.size()<20){
            return  list;
        }else {
            return list.subList(0,20);
        }
    }

    /**
     * 查询行业级联选择
     */
    @GetMapping("/tradeOptions")
    public List<TradeOptionVo> tradeOptions(CityTrade cityTrade) {
        startPage();
        List<CityTrade> list = cityTradeService.selectCityTradeList(cityTrade);
        List<TradeOptionVo> tradeOptionVoList = new ArrayList<>();
        if (!list.isEmpty()) {
            for (CityTrade c : list) {
                TradeOptionVo tradeOptionVo = new TradeOptionVo();
                tradeOptionVo.setValue(c.getTradeName());
                tradeOptionVo.setText(c.getTradeName());
                CityTradeSecond cityTradeSecond = new CityTradeSecond();
                cityTradeSecond.setTradeId(c.getTradeId());
                List<CityTradeSecond> cityTradeSecondList = cityTradeSecondService.selectCityTradeSecondList(cityTradeSecond);
                List<TradeSecondOptionVo> tradeSecondOptionVoList = new ArrayList<>();
                if (!cityTradeSecondList.isEmpty()) {
                    for (CityTradeSecond s : cityTradeSecondList) {
                        TradeSecondOptionVo tradeSecondOptionVo = new TradeSecondOptionVo();
                        tradeSecondOptionVo.setValue(s.getTradeSecondName());
                        tradeSecondOptionVo.setText(s.getTradeSecondName());
                        tradeSecondOptionVoList.add(tradeSecondOptionVo);
                    }
                }
                tradeOptionVo.setChildren(tradeSecondOptionVoList);
                tradeOptionVoList.add(tradeOptionVo);
            }
        }
        return tradeOptionVoList;
    }

    /**
     * 新增合作申请
     */
    @Log(title = "合作申请", businessType = BusinessType.INSERT)
    @PostMapping("/addCityApply")
    public AjaxResult addCityApply(@RequestBody CityApply cityApply)
    {
        cityApply.setApplyId(IdUtil.getSnowflakeNextId());
        return toAjax(cityApplyService.insertCityApply(cityApply));
    }

    /**
     * 修改项目
     */
    @Log(title = "项目", businessType = BusinessType.UPDATE)
    @GetMapping("/editCityProject")
    public AjaxResult edit(Long projectId,String text )
    {
       CityProject cityProject = cityProjectService.selectCityProjectByProjectId(projectId);
       if (cityProject!=null){
           cityProject.setProjectContent(text);
       }
       return toAjax(cityProjectService.updateCityProject(cityProject));
    }


}
